'==================================================================================================
'	Author		David Segura
'	URL			http://winpeguy.com/
'	Source		http://winpe.codeplex.com/
'	Version		20130913
'==================================================================================================
'	Purpose:
'	Copies Menu to WinPE Using Exit Script
'==================================================================================================
'	Recommended File Location in Windows:
'	%DeployRoot%\Boot\+WinPE\Menu\*
'==================================================================================================
'	Configuration:
'	Configuration is not necessary with the installation
'	Modification of the files in +WinPE\Menu\MyConfig controls all the optional settings
'==================================================================================================
'	Installation:
'	The following MDT Template files must be edited on the Deployment Server (default install path)
'		C:\Program Files\Microsoft Deployment Toolkit\Templates\Generic.xml
'		C:\Program Files\Microsoft Deployment Toolkit\Templates\LiteTouchPE.xml
'	
'	To add support, you must modify the <Exits> Section like the example below (Remove the first ' from the lines below)
'		<!-- Exits -->
'		<Exits>
'			<Exit>cscript.exe "%INSTALLDIR%\Samples\UpdateExit.vbs"</Exit>
'			<!-- WinPE Menu -->	<Exit>cscript.exe "%DEPLOYROOT%\Boot\+WinPE\Menu\Install-Menu.vbs"</Exit>
'		</Exits>
'
'	Note: You will have to Update Deployment Share (Completely regenerate the boot images) when making this change
'==================================================================================================
'	Change Log:
'	20130913	Added /ForceUnsigned to Driver Installation.  Required for x64 installation
'				This prevented MyBranding.inf and MyExpiration.inf from installing properly
'	20130911	Resolved issue with CustomBranding
'	20130910	Codeplex Beta
'==================================================================================================
Option Explicit
	Dim InstallMenu, InstallUnattend, CustomBackground, CustomBGInfo, CustomBranding
	Dim CustomExpire, CustomLogo, CustomMenu, ExtraFiles, CustomTools
	InstallMenu			= "YES"		'Contents in the +Menu subdirectory will be applied to X:\Menu
									'It recommended leaving these files stock
	InstallUnattend		= "YES"		'Contents in the +Unattend subdirectory will be applied to X:\
									'This is required to support the WinPE Menu, but it can be applied standalone as well
									'It recommended leaving these files stock
	CustomBackground	= "YES"		'Applies a Custom Background image.  Must be BMP and JPG
	CustomBGInfo		= "YES"		'Customizes BGInfo
	CustomBranding		= "YES"		'Customizes the Branding of WinPE and WinPE Menu
	CustomExpire		= "YES"		'Customizes the Expiration WinPE
	CustomLogo			= "YES"		'Customizes the Logo of WinPE Menu
	CustomMenu			= "YES"		'Contents in the MyConfig\Menu subdirectory will be applied to X:\Menu
									'Customized files may be placed in here and will be applied to WinPE
	ExtraFiles			= "YES"		'Contents in the MyConfig\ExtraFiles subdirectory will be applied to X:\
									'Architecture Specific directories are allowed
	CustomTools			= "YES"		'Tools such as Imagex.exe will be installed automatically

'Modifications below the following line are not recommended
'==================================================================================================
	Wscript.Echo "==================================================================================================="
	WScript.Echo "Starting UpdateExit from "
	WScript.Echo Wscript.ScriptFullName
	WScript.Echo "==================================================================================================="
'=================================================================================================================
Dim oShell, oEnv, objFSO, sCmd, rc

Set oShell = CreateObject("WScript.Shell")
Set oEnv = oShell.Environment("PROCESS")
Set objFSO = CreateObject("Scripting.FileSystemObject")

EnvEcho

Dim	sDeployRoot, sContent, sTemplate, sPlatform
sDeployRoot		= oEnv("DEPLOYROOT")
sContent		= oEnv("CONTENT")
sTemplate		= oEnv("TEMPLATE")
sPlatform		= oEnv("PLATFORM")
'=================================================================================================================
If oEnv("STAGE") = "WIM" then
'Customizations to the Mounted WIM
'Example:	Add files to a WIM
'CONTENT = %Temp%\MDTUpdate.*\MOUNT

'Uncomment the line below for troubleshooting purposes
'MsgBox "Pause in Stage " & oEnv("STAGE") & " invoked from " & Wscript.ScriptFullName

	Dim ThisDirectory, CopyFrom, CopyTo
	ThisDirectory = Replace(WScript.ScriptFullName, WScript.ScriptName, "")

	Wscript.Echo "==================================================================================================="
	
	'Need to create X:\Menu if it does not exist
	If Not objFSO.FolderExists(sContent & "\Menu") Then
		WScript.Echo "WinPE Menu > Creating " & sContent & "\Menu"
		objFSO.CreateFolder(sContent & "\Menu")
	Else
		WScript.Echo "WinPE Menu > Directory " & sContent & "\Menu already exists"
	End If


	If InstallMenu = "YES" Then
		'MsgBox "InstallMenu Start"
		If objFSO.FolderExists(ThisDirectory & "+Menu") Then
			WScript.Echo "WinPE Menu > Adding Core Menu Files"
			CopyFrom	= ThisDirectory & "+Menu"
			CopyTo		= sContent & "\Menu"
			WScript.Echo "WinPE Menu > Copying Menu Content from " & CopyFrom
			WScript.Echo "WinPE Menu > Copying Menu Content to " & CopyTo
			objFSO.CopyFolder CopyFrom , CopyTo , True
		Else
			WScript.Echo "Core Menu Not Found!!!!!!!!!!!!"
		End If
		'MsgBox "InstallMenu End"
	End If
	
	If InstallUnattend = "YES" Then
		'MsgBox "InstallUnattend Start"
		WScript.Echo "WinPE Unattend > Adding Unattend.vbs Support"
		If Not objFSO.FileExists(sContent & "\Unattend_" & sTemplate & ".xml") Then WScript.Echo "WinPE Unattend > Creating Marker Unattend_" & sTemplate & ".xml"
		If Not objFSO.FileExists(sContent & "\Unattend_" & sTemplate & ".xml") Then objFSO.MoveFile sContent & "\Unattend.xml" , sContent & "\Unattend_" & sTemplate & ".xml"

		If objFSO.FileExists(sContent & "\Unattend.vbs") Then WScript.Echo "WinPE Unattend > Deleting Existing Unattend.vbs"
		If objFSO.FileExists(sContent & "\Unattend.vbs") Then objFSO.DeleteFile(sContent & "\Unattend.vbs")
		
		If objFSO.FileExists(sContent & "\Unattend.xml") Then WScript.Echo "WinPE Unattend > Deleting Existing Unattend.xml"
		If objFSO.FileExists(sContent & "\Unattend.xml") Then objFSO.DeleteFile(sContent & "\Unattend.xml")
		
		WScript.Echo "WinPE Unattend > Copying updated Unattend.vbs and Unattend.xml"
		objFSO.CopyFile ThisDirectory & "+Unattend\Unattend.vbs" , sContent & "\", True
		objFSO.CopyFile ThisDirectory & "+Unattend\Unattend_" & sPlatform & ".xml" , sContent & "\", True
		objFSO.MoveFile sContent & "\Unattend_" & sPlatform & ".xml" , sContent & "\Unattend.xml"
		WScript.Echo "WinPE Unattend > Unattend Integration Complete"
		'MsgBox "InstallUnattend End"
	End If
	
	If CustomBackground = "YES" Then
		'MsgBox "CustomBackground Start"
		If objFSO.FileExists(sContent & "\Windows\System32\winpe.bmp") Then WScript.Echo "WinPE Background > Deleting Existing winpe.bmp"
		If objFSO.FileExists(sContent & "\Windows\System32\winpe.bmp") Then objFSO.DeleteFile(sContent & "\Windows\System32\winpe.bmp")
		If objFSO.FileExists(sContent & "\Windows\System32\winpe.jpg") Then WScript.Echo "WinPE Background > Deleting Existing winpe.jpg"
		If objFSO.FileExists(sContent & "\Windows\System32\winpe.jpg") Then objFSO.DeleteFile(sContent & "\Windows\System32\winpe.jpg")

		If objFSO.FileExists(ThisDirectory & "MyConfig\MyBackground.bmp") Then
			WScript.Echo "WinPE Background > Applying Custom MyBackground.bmp"
			objFSO.CopyFile ThisDirectory & "MyConfig\MyBackground.bmp" , sContent & "\Windows\System32\", True
		Else
			WScript.Echo "WinPE Background > Applying Default MyBackground.bmp"
			objFSO.CopyFile ThisDirectory & "+Config\MyBackground.bmp" , sContent & "\Windows\System32\", True
		End If

		If objFSO.FileExists(ThisDirectory & "MyConfig\MyBackground.jpg") Then
			WScript.Echo "WinPE Background > Applying Custom MyBackground.jpg"
			objFSO.CopyFile ThisDirectory & "MyConfig\MyBackground.jpg" , sContent & "\Windows\System32\", True
		Else
			WScript.Echo "WinPE Background > Applying Default MyBackground.jpg"
			objFSO.CopyFile ThisDirectory & "+Config\MyBackground.jpg" , sContent & "\Windows\System32\", True
		End If

		objFSO.MoveFile sContent & "\Windows\System32\MyBackground.bmp" , sContent & "\Windows\System32\winpe.bmp"
		objFSO.MoveFile sContent & "\Windows\System32\MyBackground.jpg" , sContent & "\Windows\System32\winpe.jpg"
		'MsgBox "CustomBackground End"
	End If
	
	If CustomBGInfo = "YES" Then
		'MsgBox "CustomBGInfo Start"
			If objFSO.FileExists(sContent & "\Windows\System32\BGInfo.bgi") Then
				WScript.Echo "WinPE BGInfo > Deleting Existing BGInfo.bgi"
				objFSO.DeleteFile(sContent & "\Windows\System32\BGInfo.bgi")
			End If
			If objFSO.FileExists(sContent & "\Windows\System32\MyBGInfo.bgi") Then
				WScript.Echo "WinPE BGInfo > Deleting Existing MyBGInfo.bgi"
				objFSO.DeleteFile(sContent & "\Windows\System32\MyBGInfo.bgi")
			End If
			If objFSO.FileExists(ThisDirectory & "MyConfig\MyBGInfo.bgi") Then
				WScript.Echo "WinPE BGInfo > Applying Custom BGInfo.bgi"
				objFSO.CopyFile ThisDirectory & "MyConfig\MyBGInfo.bgi" , sContent & "\Windows\System32\", True
			ElseIf objFSO.FileExists(ThisDirectory & "+Config\MyBGInfo.bgi") Then
				WScript.Echo "WinPE BGInfo > Applying Default BGInfo.bgi"
				objFSO.CopyFile ThisDirectory & "+Config\MyBGInfo.bgi" , sContent & "\Windows\System32\", True
			End If
		If objFSO.FileExists(sContent & "\Windows\System32\MyBGInfo.bgi") Then objFSO.MoveFile sContent & "\Windows\System32\MyBGInfo.bgi" , sContent & "\Windows\System32\BGInfo.bgi"
		
		If Not objFSO.FileExists(sContent & "\Windows\System32\BGInfo.exe") Then
			WScript.Echo "WinPE BGInfo > Installing BGInfo.exe from MDT Tools"
			If Lcase(sPlatform) = "x86" Then
				objFSO.CopyFile sDeployRoot & "\Tools\" & sPlatform & "\BGInfo.exe" , sContent & "\Windows\System32\", True
			ElseIf Lcase(sPlatform) = "x64" Then
				objFSO.CopyFile sDeployRoot & "\Tools\" & sPlatform & "\BGInfo64.exe" , sContent & "\Windows\System32\", True
				objFSO.MoveFile sContent & "\Windows\System32\BGInfo64.exe" , sContent & "\Windows\System32\BGInfo.exe"
			End If
		End If
		'MsgBox "CustomBGInfo End"
	End If
	
	If CustomBranding = "YES" Then
		'MsgBox "CustomBranding Start"
		If objFSO.FileExists(ThisDirectory & "MyConfig\MyBranding.inf") Then
			WScript.Echo "WinPE Custom Branding > Applying Custom Menu Branding"
			sCmd = "dism.exe /image:""" & oEnv("CONTENT") & """ /Add-Driver /Driver:""" & ThisDirectory & "MyConfig\MyBranding.inf""" & " /ForceUnsigned"
			WScript.Echo "WinPE Custom Branding > Running Command: " & sCmd
			rc = oShell.Run(sCmd, 0, True)
			WScript.Echo "WinPE Custom Branding > Exit code = " & CStr(rc)
		Else
			'WScript.Echo "WinPE Custom Branding > Applying Default Menu Branding"
			'sCmd = "dism.exe /image:""" & oEnv("CONTENT") & """ /Add-Driver /Driver:""" & ThisDirectory & "+Config\MyBranding.inf""" & " /ForceUnsigned"
			'WScript.Echo "WinPE Custom Branding > Running Command: " & sCmd
			'rc = oShell.Run(sCmd, 0, True)
			'WScript.Echo "WinPE Custom Branding > Exit code = " & CStr(rc)
		End If
		'MsgBox "CustomBranding End"
	End If

	If CustomExpire = "YES" Then
		'MsgBox "CustomExpire Start"
		If objFSO.FileExists(ThisDirectory & "MyConfig\MyExpiration.inf") Then
			WScript.Echo "WinPE Expiration > Applying WinPE Expiration"
			sCmd = "dism.exe /image:""" & oEnv("CONTENT") & """ /Add-Driver /Driver:""" & ThisDirectory & "MyConfig\MyExpiration.inf""" & " /ForceUnsigned"
			WScript.Echo "WinPE Expiration > Running Command: " & sCmd
			rc = oShell.Run(sCmd, 0, True)
			WScript.Echo "WinPE Expiration > Exit code = " & CStr(rc)
		End If
		'MsgBox "CustomExpire End"
	End If
	
	
	If CustomLogo = "YES" Then
		'MsgBox "CustomLogo Start"
			If objFSO.FileExists(ThisDirectory & "MyConfig\MyLogo.png") Then
				WScript.Echo "WinPE Custom Logo > Deleting Existing Logo.png"
				objFSO.DeleteFile(sContent & "\Menu\Logo.png")
				WScript.Echo "WinPE Custom Logo > Applying Custom MyLogo.png"
				objFSO.CopyFile ThisDirectory & "MyConfig\MyLogo.png" , sContent & "\Menu\", True
				objFSO.MoveFile sContent & "\Menu\MyLogo.png" , sContent & "\Menu\Logo.png"
			End If
		'MsgBox "CustomLogo End"
	End If
	
	If CustomMenu = "YES" Then
		'MsgBox "CustomMenu Start"
		If objFSO.FolderExists(ThisDirectory & "MyConfig\Menu") Then
			WScript.Echo "WinPE Custom Menu > Adding Custom Menu Files"
			CopyFrom	= ThisDirectory & "MyConfig\Menu"
			CopyTo		= sContent & "\Menu"
			WScript.Echo "WinPE Custom Menu > Copying Menu Content from " & CopyFrom
			WScript.Echo "WinPE Custom Menu > Copying Menu Content to " & CopyTo
			objFSO.CopyFolder CopyFrom , CopyTo , True
		Else
			WScript.Echo "Skipping Custom Menu Files"
		End If
		'MsgBox "CustomMenu End"
	End If
	
	If ExtraFiles = "YES" Then
		'MsgBox "ExtraFiles Start"
		If objFSO.FolderExists(ThisDirectory & "+Config\ExtraFiles\WinPE") Then
			WScript.Echo "WinPE Extra Files > Adding Default Extra Files"
			CopyFrom	= ThisDirectory & "+Config\ExtraFiles\WinPE"
			CopyTo		= sContent
			WScript.Echo "WinPE Extra Files > Copying ExtraFiles Content from " & CopyFrom
			WScript.Echo "WinPE Extra Files > Copying ExtraFiles Content to " & CopyTo
			objFSO.CopyFolder CopyFrom , CopyTo , True
		End If
		If objFSO.FolderExists(ThisDirectory & "+Config\ExtraFiles\WinPE" & sPlatform) Then
			WScript.Echo "WinPE Extra Files > Adding Default " & sPlatform & " Extra Files (Architecture Specific)"
			CopyFrom	= ThisDirectory & "+Config\ExtraFiles\WinPE" & sPlatform
			CopyTo		= sContent
			WScript.Echo "WinPE Extra Files > Copying ExtraFiles Content from " & CopyFrom
			WScript.Echo "WinPE Extra Files > Copying ExtraFiles Content to " & CopyTo
			objFSO.CopyFolder CopyFrom , CopyTo , True
		End If
		If objFSO.FolderExists(ThisDirectory & "MyConfig\ExtraFiles\WinPE") Then
			WScript.Echo "WinPE Extra Files > Adding Default Extra Files"
			CopyFrom	= ThisDirectory & "MyConfig\ExtraFiles\WinPE"
			CopyTo		= sContent
			WScript.Echo "WinPE Extra Files > Copying ExtraFiles Content from " & CopyFrom
			WScript.Echo "WinPE Extra Files > Copying ExtraFiles Content to " & CopyTo
			objFSO.CopyFolder CopyFrom , CopyTo , True
		End If
		If objFSO.FolderExists(ThisDirectory & "MyConfig\ExtraFiles\WinPE" & sPlatform) Then
			WScript.Echo "WinPE Extra Files > Adding Default " & sPlatform & "Extra Files (Architecture Specific)"
			CopyFrom	= ThisDirectory & "MyConfig\ExtraFiles\WinPE" & sPlatform
			CopyTo		= sContent
			WScript.Echo "WinPE Extra Files > Copying ExtraFiles Content from " & CopyFrom
			WScript.Echo "WinPE Extra Files > Copying ExtraFiles Content to " & CopyTo
			objFSO.CopyFolder CopyFrom , CopyTo , True
		End If
		'MsgBox "ExtraFiles End"
	End If
	
	If CustomTools = "YES" Then
		'MsgBox "CustomTools Start"
		If Not objFSO.FileExists(sContent & "\Windows\System32\Imagex.exe") And objFSO.FileExists(sDeployRoot & "\Tools\" & sPlatform & "\Imagex.exe") Then
			WScript.Echo "WinPE Tools > Installing Imagex.exe from MDT Tools"
			objFSO.CopyFile sDeployRoot & "\Tools\" & sPlatform & "\Imagex.exe" , sContent & "\Windows\System32\", True
		End If
		If Not objFSO.FileExists(sContent & "\Windows\System32\Imagex.exe") And objFSO.FileExists(sDeployRoot & "\Servicing\" & sPlatform & "\Imagex.exe") Then
			WScript.Echo "WinPE Tools > Installing Imagex.exe from MDT Servicing"
			objFSO.CopyFile sDeployRoot & "\Servicing\" & sPlatform & "\Imagex.exe" , sContent & "\Windows\System32\", True
		End If
		If Not objFSO.FileExists(sContent & "\Windows\System32\Wimscript.ini") And objFSO.FileExists(sDeployRoot & "\Tools\" & sPlatform & "\Wimscript.ini") Then
			WScript.Echo "WinPE Tools > Installing Wimscript.ini from MDT Tools"
			objFSO.CopyFile sDeployRoot & "\Tools\" & sPlatform & "\Wimscript.ini" , sContent & "\Windows\System32\", True
		End If
		If Not objFSO.FileExists(sContent & "\Windows\System32\Wimscript.ini") And objFSO.FileExists(sDeployRoot & "\Servicing\" & sPlatform & "\Wimscript.ini") Then
			WScript.Echo "WinPE Tools > Installing Wimscript.ini from MDT Servicing"
			objFSO.CopyFile sDeployRoot & "\Servicing\" & sPlatform & "\Wimscript.ini" , sContent & "\Windows\System32\", True
		End If
		'MsgBox "CustomTools End"
	End If
	
	WScript.Echo "WinPE Menu Installation Complete"
	
	'Uncomment the line below for troubleshooting purposes
	'MsgBox "Pause in Stage " & oEnv("STAGE") & " invoked from " & Wscript.ScriptFullName
	
	WScript.Quit 1
End if
'=================================================================================================================
If oEnv("STAGE") = "POSTWIM" then
'Customization after the WIM has been finalized
'Example:	Copy WIM to WDS
'CONTENT = %Temp%\MDTUpdate.*\LiteTouchPE_x86.wim or LiteTouchPE_x64.wim or Generic_x86.wim or Generic_x64.wim

	WScript.Quit 1
End if
'=================================================================================================================
If oEnv("STAGE") = "ISO" then
'Customizations to the Flat ISO Directory
'Example:	Add files to an ISO
'CONTENT = %Temp%\MDTUpdate.*\ISO (Flat directory of the ISO before it is created)

	WScript.Quit 1
End if
'=================================================================================================================
If oEnv("STAGE") = "POSTISO" then
'Customizations after the ISO has been created
'Example:	Copy ISO to Hyper-V
'CONTENT = %Temp%\MDTUpdate.*\LitetouchPE_x86.iso or LitetouchPE_x64.iso or Generic_x86.iso or Generic_x86.iso

	WScript.Quit 1
End if
'=================================================================================================================
Sub EnvEcho
	'Write out each of the passed-in environment variable values
	WScript.Echo "INSTALLDIR = " & oEnv("INSTALLDIR")		'INSTALLDIR		= This will return the Path to your MDT Installation similar to C:\Program Files\Microsoft Deployment Toolkit
	WScript.Echo "DEPLOYROOT = " & oEnv("DEPLOYROOT")		'DEPLOYROOT		= This will return the Path to your Deployment Share similar to C:\DeploymentShare
	WScript.Echo "PLATFORM = " & oEnv("PLATFORM")			'PLATFORM 		= x86 \ x64
	WScript.Echo "ARCHITECTURE = " & oEnv("ARCHITECTURE")	'ARCHITECTURE	= x86 \ amd64
	WScript.Echo "TEMPLATE = " & oEnv("TEMPLATE")			'TEMPLATE 		= LiteTouchPE \ Generic
	WScript.Echo "STAGE = " & oEnv("STAGE")					'STAGE 			= WIM \ POSTWIM \ ISO \ POSTISO
	WScript.Echo "CONTENT = " & oEnv("CONTENT")				'CONTENT		= WIM Stage will return the Mount directory 		%Temp%\MDTUpdate.*\MOUNT
															'CONTENT		= POSTWIM Stage will return the WIM file name		%Temp%\MDTUpdate.*\LiteTouchPE_x86.wim
															'CONTENT		= ISO Stage will return the ISO directory			%Temp%\MDTUpdate.*\ISO
															'CONTENT		= POSTISO Stage will return the ISO file name		%Temp%\MDTUpdate.*\LitetouchPE_x86.iso
End Sub
'=================================================================================================================
'Add Functions or Subroutines below this entry